package editor.osx;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseAdapter;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.JSeparator;

/**
 *
 * @author Michael Hanns
 *
 */
public class PopUp {

    private WITNAFrameView parent;
    private JPopupMenu popup;
    
    private int x = -1;
    private int y = -1;
    private int z = -1;

    public void create(WITNAFrameView parent) {
        this.parent = parent;
        popup = new JPopupMenu();
        JMenuItem menuItem = new JMenuItem("Select");
        menuItem.addActionListener(new SelectListener());
        popup.add(menuItem);
        menuItem = new JMenuItem("New");
        menuItem.addActionListener(new NewListener());
        popup.add(menuItem);
        menuItem = new JMenuItem("Copy/Paste");
        menuItem.addActionListener(new CopyListener());
        popup.add(menuItem);
        menuItem = new JMenuItem("Delete");
        menuItem.addActionListener(new DeleteListener());
        popup.add(menuItem);
        popup.add(new JSeparator());
        JMenu addMenu = new JMenu("Add..");
        menuItem = new JMenuItem("Item");
        menuItem.addActionListener(new AddItemListener());
        addMenu.add(menuItem);
        menuItem = new JMenuItem("Mobile");
        menuItem.addActionListener(new AddMobileListener());
        addMenu.add(menuItem);
        menuItem = new JMenuItem("Trigger");
        menuItem.addActionListener(new AddTriggerListener());
        addMenu.add(menuItem);
        popup.add(addMenu);
        
        popup.add(new JSeparator());
        menuItem = new JMenuItem("Set initial spawn");
        menuItem.addActionListener(new SetSpawnListener());
        popup.add(menuItem);

        //Add listener to components that can bring up popup menus.
        MouseListener popupListener = new PopupListener();
        parent.setPopUpListener(popupListener);
    }

    class SelectListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            parent.selectRoom(x, y, z);
        }
        
    }
    
    class NewListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            parent.newRoom(x, y, z);
        }
        
    }

    
    class CopyListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            parent.copyRoom(x, y, z);
        }
        
    }

    
    class DeleteListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            parent.deleteRoom(x, y, z);
        }
        
    }

    
    class AddItemListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            parent.selectRoom(x, y, z);
            parent.addItem();
        }
        
    }

    
    class AddMobileListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            parent.selectRoom(x, y, z);
            parent.addMobile();
        }
        
    }

    
    class AddTriggerListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            parent.selectRoom(x, y, z);
            parent.addTrigger();
        }
        
    }

    class SetSpawnListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            parent.setSpawn(x, y, z);
            parent.setUnsavedChanges(true);
            parent.drawFloorToBuffer();
            parent.drawBuffer();
        }
        
    }

    class PopupListener extends MouseAdapter {

        @Override
        public void mousePressed(MouseEvent evt) {
            maybeShowPopup(evt);
        }

        @Override
        public void mouseReleased(MouseEvent evt) {
            maybeShowPopup(evt);
        }

        private void maybeShowPopup(MouseEvent evt) {
            if (evt.isPopupTrigger()) {
                parent.setMode(0);
                popup.show(evt.getComponent(),
                        evt.getX(), evt.getY());
                x = evt.getX() / 50;
                y = 100 - (evt.getY() / 50) - 1;
                z = parent.getFloor();
            }
        }
    }
}
